<?php
/**
 * 订单表
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/22
 * Time: 14:11
 */

namespace Resource\Service;
use Think\Model;

class OrderService extends Model
{
    protected $trueTableName = 'wz_order';
    /**
     * @param $params
     * @return array|bool
     */
    public function addOrder($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $id = D('Resource/Order')->addOrder($params);
        if($id) {
            return $id;
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getOrderInfo($params, $fields='') {
        $dataInfo = D('Resource/Order')->getOrderInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchOrderList($params) {
        $page = 1;
        $page_size = 15;
        $start = isset($params['start'])?$params['start']:'';
        $end = isset($params['end'])?$params['end']:'';

        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['id']) && $data['id'] =$params['id'];
        isset($params['status']) && $data['status'] =$params['status'];
        isset($params['admin_id']) && $data['admin_id'] =$params['admin_id'];
        isset($params['department_id']) && $data['department_id'] =$params['department_id'];
        isset($params['name']) && $data['name'] = ['like',"%{$params['name']}%"];
        isset($params['order_code']) && $data['order_code'] = ['like',"%{$params['order_code']}%"];
        isset($params['admin_name']) && $data['admin_name'] = ['like',"%{$params['admin_name']}%"];
        isset($params['company_name']) && $data['company_name'] = ['like',"%{$params['company_name']}%"];

        if(isset($start) && $start){
           $data['create_time'] = array(array('gt',$start),array('lt',$end)) ;
        }
        //查询总数
        $total = D('Resource/Order')->getOrderCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $orderList = D('Resource/Order')->getOrderList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['orderList'] = $orderList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateOrderInfo($params) {
        return D('Resource/Order')->updateOrderInfo($params);
    }

    /**
 * @param $params
 * @return mixed
 */
    public function getAllOrderList($params){
        $start = isset($params['start'])?$params['start']:'';
        $end = isset($params['end'])?$params['end']:'';
        $data = [];
        isset($params['admin_id']) && $data['admin_id'] =$params['admin_id'];

        if(isset($start) && $start){
            $data['create_time'] = array(array('gt',$start),array('lt',$end)) ;
        }
        $dataInfo = D('Resource/Order')->getAllOrderList($data);
        return $dataInfo;
    }

    /**
     * 业支订单
     * @param $params
     * @return mixed
     */
    public function getJoinOrderList($params){

        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['status']) && $data['o.status'] =$params['status'];
        isset($params['department_name']) && $data['d.name'] = ['like',"%{$params['department_name']}%"];
        isset($params['admin_name']) && $data['admin_name'] = ['like',"%{$params['admin_name']}%"];
        isset($params['company_name']) && $data['c.company_name'] = ['like',"%{$params['company_name']}%"];

        //查询总数
        $total = D('Resource/Order')->getJoinCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $orderList = D('Resource/Order')->getJoinOrderList($data,$page,$page_size);
        $order_status = C('ORDER_STATUS');
        foreach ($orderList as $k=>$v){
            $orderList[$k]['status'] = $order_status[$v['status']];
        }

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['orderList'] = $orderList;
        return $result;

    }
    /**
     * 查询公司下订单
     * @param $params
     * @return array
     */
    public function searchOrderListByCompany($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['company_name']) && $data['company_name'] = $params['company_name'];

        //查询总数
        $total = D('Resource/Order')->getOrderCountByCompany($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询列表
        $dataList = D('Resource/Order')->getOrderListByCompany($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['orderList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getStatOrderInfo($params){
        $dataInfo = D('Resource/Order')->getStatOrderInfo($params);
        return $dataInfo?$dataInfo['0']:[];
    }

    /**
     * 查询订单总数
     * @param $params
     * @return int
     */
    public function getOrderCount($params){
        //查询总数
        $total = D('Resource/Order')->getOrderCount($params);
        return intval($total);
    }
}